﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;

namespace wd_30_single_instance_v1
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public static Mutex mutex1; //静态全局，避免被.NET自动回收

        public static string CLASS_NAME = "HwndWrapper[wd-30-single-instance-v1.exe;;0d15507d-5b01-40c0-8e78-19d044ef6f57]";
        protected override void OnStartup(StartupEventArgs e)
        {
            //https://blog.csdn.net/as_chengge/article/details/100529817
            base.OnStartup(e);

            IntPtr hwnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, null, "mywindow2022");
            
            //参数1必须是null，如果是""会查不到句柄。
            hwnd = FindWindow(null, "mywindow2022");

            mutex1 = new Mutex(true, "{9A7141FB-45DF-4F76-90DF-7F9CF284E4FD}", out bool isFirstInstance);
            if (isFirstInstance)
            {
                //Shutdown();
                Trace.WriteLine("1st time");
            }
            else
            {
                Shutdown();

                MessageBox.Show("已运行！");
                //hwnd = new WindowInteropHelper(this).Handle;
                if (hwnd.ToInt32()>0)
                {
                    //SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
                    SetForegroundWindow(hwnd);
                }                
            }
        }

        [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);


        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
        private static extern bool SetForegroundWindow(IntPtr hwnd);

        public static int SW_SHOW = 5;
        public static int SW_NORMAL = 1;
        public static int SW_MAX = 3;
        public static int SW_HIDE = 0;
        public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);    //窗体置顶
        public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);    //取消窗体置顶
        public const uint SWP_NOMOVE = 0x0002;    //不调整窗体位置
        public const uint SWP_NOSIZE = 0x0001;    //不调整窗体大小
        public bool isFirst = true;

        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        public static extern bool ShowWindow(System.IntPtr hWnd, int nCmdShow);

        //根据窗口类型或者窗口标题获取窗口句柄
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        //————————————————
        //版权声明：本文为CSDN博主「魏军强」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
        //原文链接：https://blog.csdn.net/u014434080/article/details/51029959

    }
}
